<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('crypt.inc');
require_once('settings.inc');
require_once('menu.inc');

function NewPj($unum, $headline, $menu, $rstufe) {
	global $pmlight;
	$db = OpenDB();

	$num = $_REQUEST['num'];
	$name = $_REQUEST['name'];
	$pl = $_REQUEST['pl'];
	$plav = $_REQUEST['plav'];
	$sgf = $_REQUEST['sgf'];
	$tmgroup = $_REQUEST['tmgroup'];
	$status = $_REQUEST['status'];
?>
<p class="cry">Neues Projekt anlegen</p>
<form name="edit" action="edit_pj.php" method="post">
<input type="hidden" name="unum" value="<? echo "$unum"; ?>">
<input type="hidden" name="headline" value="12">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="rstufe" value="<? echo "$rstufe"; ?>">
<input type="hidden" name="func" value="CheckNewPj">
<br>
<table class="indent"><tr><td>
<?php
	ButtonLink("Zur&uuml;ck", "forms.php", "unum=$unum&headline=$headline&menu=$menu&rstufe=$rstufe");
//echo "<a href=\"forms.php?unum=$unum&headline=$headline&menu=$menu&rstufe=$rstufe\"><img src=\"image/button_back.png\" border=1 alt=\"Zurueck\"></a>\n";
?>
</td></tr></table>
<table class="input">
   <tr>
      <td>Projektnummer:</td>
      <td><input type="text" class="inputmust" name="num" value="<? echo "$num"; ?>" size=8 maxlength=8></td>
   </tr>
   <tr>
      <td>Bezeichnung:</td>
      <td><input type="text" class="inputmust" name="name" value="<? echo "$name"; ?>" size=60 maxlength=100></td>
   </tr>
   <tr>
      <td>Projektleiter:</td>
      <td><select name="pl"><?php
	$query = "select mi_num, mi_vname, mi_nname from mitarbeiter where ";
	$query .= "mi_rstufe in (1,2) and mi_plan = true ";
	$query .= "order by mi_nname, mi_vname";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);
	$row = 0;

	while ($row < $numrows) {
	   $data = fetchDB($result, $row);
	   $mi_num = $data[0];
	   $mi_vname = $data[1];
	   $mi_nname = $data[2];
	   echo "<option value=\"$mi_num\"";

	   if ($pl == $mi_num)
	      echo " selected";

	   echo ">$mi_nname $mi_vname</option>\n";
	   $row++;
	}
?>
         </select>
      </td>
   </tr>
<?
	if (!$pmlight) {
?>
   <tr>
      <td>PLA-V:</td>
      <td><select name="plav"><?php
	$query = "select mi_num, mi_vname, mi_nname from mitarbeiter where ";
	$query .= "mi_rstufe in (1,2) and mi_plan = true ";
	$query .= "order by mi_nname, mi_vname";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);
	$row = 0;

	while ($row < $numrows) {
	   $data = fetchDB($result, $row);
	   $mi_num = $data[0];
	   $mi_vname = $data[1];
	   $mi_nname = $data[2];

	   echo "<option value=\"$mi_num\"";

	   if ($plav == $mi_num)
	      echo " selected";

	   echo ">$mi_nname $mi_vname</option>\n";
	   $row++;
	}
?>
         </select>
      </td>
   </tr>
   <tr>
      <td>Auftraggeber:</td>
      <td><select name="sgf"><?php
	$query = "select ks_num, ks_sgf from key_sgf order by ks_sgf";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);
	$row = 0;

	while ($row < $numrows) {
	   $data = fetchDB($result, $row);
	   $ks_num = $data[0];
	   $ks_sgf = $data[1];
	   echo "<option value=\"$ks_num\"";

	   if ($sgf == $ks_num)
	      echo " selected";

	   echo ">$ks_sgf</option>\n";
	   $row++;
	}
?>
         </select>
      </td>
   </tr>
   <tr>
      <td>Themenmanagergruppe:</td>
      <td><select name="tmgroup"><?php
	$query = "select kt_num, kt_tm from key_tmgroup order by kt_tm";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);
	$row = 0;

	while ($row < $numrows) {
	   $data = fetchDB($result, $row);
	   $kt_num = $data[0];
	   $kt_tm = $data[1];
	   echo "<option value=\"$kt_num\"";

	   if ($tmgroup == $kt_num)
	      echo " selected";

	   echo ">$kt_tm</option>\n";
	   $row++;
	}
?>
         </select>
      </td>
   </tr>
<?
	}
?>
   <tr>
      <td>Projektart:</td>
      <td><select name="status">
             <option value="0">Projekt</option>
             <option value="1"<? if ($status == 1) echo " selected"; ?>>Konto</option>
          </select>
      </td>
   </tr>
<?
	if ($pmlight) {
	   echo "<input type=\"hidden\" name=\"plav\" value=\"$unum\">\n";
	   echo "<input type=\"hidden\" name=\"sgf\" value=\"1\">\n";
	   echo "<input type=\"hidden\" name=\"tmgroup\" value=\"1\">\n";
	}
?>
</table>
<table class="indent"><tr><td>
<?
	ButtonSubmit("Speichern", "edit");
?>
</td></tr></table>
</form>
<?
	closeDB($db);
}

function EditPj($pjnum, $unum, $headline, $menu, $rstufe) {
	global $pmlight;
	global $role;
	$db = OpenDB();

	if ($pjnum > 0) {
	   $query = "select pr_name, pr_pl, pr_plav, pr_sgf, pr_tmgroup, pr_status ";
	   $query = $query . "from project where pr_num = $pjnum";
	   $result = QueryDB($db, $query);
	   $numrows = numrowsDB($result);

	   if ($numrows != 1) {
	      Error("Error: Projekt \"$pjnum\" wurde nicht gefunden!\n");
	      return;
	   }

	   $data = fetchDB($result, 0);
	   $pr_num = $pjnum;
	   $pr_name = $data[0];
	   $pr_pl = $data[1];
	   $pr_plav = $data[2];
	   $pr_sgf = $data[3];
	   $pr_tmgroup = $data[4];
	   $pr_status = $data[5];
	   echo "<p class=\"cry\">Projekt Editieren</p>\n";
	} else {
	   $pr_num = $_REQUEST['num'];
	   $pr_name = $_REQUEST['name'];
	   $pr_pl = $_REQUEST['pl'];
	   $pr_plav = $_REQUEST['plav'];
	   $pr_sgf = $_REQUEST['sgf'];
	   $pr_tmgroup = $_REQUEST['tmgroup'];
	   $pr_status = $_REQUEST['status'];
	   $func = $_REQUEST['func'];

	   if ($func != "matoprup")
	      echo "<p class=\"cry\">Eingabefehler!</p>\n";
	}

	$matopr = $_REQUEST['matopr'];
?>
<form name="edit" action="edit_pj.php" method="post">
<input type="hidden" name="unum" value="<? echo "$unum"; ?>">
<?php
	if ($pjnum <= 0) {
	   echo "<input type=\"hidden\" name=\"headline\" value=\"12\">\n";
	} else {
	   echo "<input type=\"hidden\" name=\"headline\" value=\"13\">\n";
	}
?>
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="rstufe" value="<? echo "$rstufe"; ?>">
<input type="hidden" name="minum" value="<? echo "$pjnum"; ?>">
<input type="hidden" name="status" value="<? echo "$pr_status"; ?>">
<input type="hidden" name="num" value="<? echo "$pr_num"; ?>">
<input type="hidden" name="func" value="CheckEditPj">
<br>
<table class="indent"><tr><td>
<?php
	ButtonLink("Zur&uuml;ck", "forms.php", "unum=$unum&headline=$headline&menu=$menu&rstufe=$rstufe");
?>
</td></tr></table>
<table border=0 cellspacing=0 cellpadding=0><tr><td valign="top">
<table class="input">
   <tr>
      <td>Projektnummer:</td>
      <td><input type="text" class="inputmust" name="num" value="<? echo "$pr_num"; ?>" disabled size=8 maxlength=8></td>
   </tr>
   <tr>
      <td>Bezeichnung:</td>
      <td><input type="text" class="inputmust" name="name" value="<? echo "$pr_name"; ?>" size=60 maxlength=100></td>
   </tr>
   <tr>
      <td>Projektleiter:</td>
      <td><select name="pl"><?php
	$query = "select mi_num, mi_vname, mi_nname from mitarbeiter where ";
	$query .= "mi_rstufe in (1,2) and mi_plan = true ";
	$query .= "order by mi_nname, mi_vname";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);
	$row = 0;

	while ($row < $numrows) {
	   $data = fetchDB($result, $row);
	   $mi_num = $data[0];
	   $mi_vname = $data[1];
	   $mi_nname = $data[2];

	   if ($pr_pl == $mi_num) {
	      echo "<option selected value=\"$mi_num\">$mi_nname $mi_vname</option>\n";
	   } else {
	      echo "<option value=\"$mi_num\">$mi_nname $mi_vname</option>\n";
	   }

	   $row++;
	}
?>
         </select>
      </td>
   </tr>
<?
	if (!$pmlight) {
?>
   <tr>
      <td>PLA-V:</td>
      <td><select name="plav"><?php
	$query = "select mi_num, mi_vname, mi_nname from mitarbeiter where ";
	$query .= "mi_rstufe in (1,2) and mi_plan = true ";
	$query .= "order by mi_nname, mi_vname";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);
	$row = 0;

	while ($row < $numrows) {
	   $data = fetchDB($result, $row);
	   $mi_num = $data[0];
	   $mi_vname = $data[1];
	   $mi_nname = $data[2];

	   if ($pr_plav == $mi_num) {
	      echo "<option selected value=\"$mi_num\">$mi_nname $mi_vname</option>\n";
	   } else {
	      echo "<option value=\"$mi_num\">$mi_nname $mi_vname</option>\n";
	   }

	   $row++;
	}
?>
         </select>
      </td>
   </tr>
   <tr>
      <td>Auftraggeber:</td>
      <td><select name="sgf"><?php
	$query = "select ks_num, ks_sgf from key_sgf order by ks_sgf";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);
	$row = 0;

	while ($row < $numrows) {
	   $data = fetchDB($result, $row);
	   $ks_num = $data[0];
	   $ks_sgf = $data[1];

	   if ($ks_num == $pr_sgf) {
	      echo "<option selected value=\"$ks_num\">$ks_sgf</option>\n";
	   } else {
	      echo "<option value=\"$ks_num\">$ks_sgf</option>\n";
	   }

	   $row++;
	}
?>
         </select>
      </td>
   </tr>
   <tr>
      <td>Themenmanagergruppe:</td>
      <td><select name="tmgroup"><?php
	$query = "select kt_num, kt_tm from key_tmgroup order by kt_tm";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);
	$row = 0;

	while ($row < $numrows) {
	   $data = fetchDB($result, $row);
	   $kt_num = $data[0];
	   $kt_tm = $data[1];

	   if ($kt_num == $pr_tmgroup) {
	      echo "<option selected value=\"$kt_num\">$kt_tm</option>\n";
	   } else {
	      echo "<option value=\"$kt_num\">$kt_tm</option>\n";
	   }

	   $row++;
	}
?>
         </select>
      </td>
   </tr>
<?
	}
?>
   <tr>
      <td>Projektart:</td>
      <td><select name="status" disabled>
             <option value="0">Projekt</option>
             <option value="1" <? if (isset($pr_status) && $pr_status) echo "selected"; ?>>Konto</option>
          </select>
      </td>
   </tr>
<?
	if ($pmlight) {
	   echo "<input type=\"hidden\" name=\"plav\" value=\"$unum\">\n";
	   echo "<input type=\"hidden\" name=\"sgf\" value=\"1\">\n";
	   echo "<input type=\"hidden\" name=\"tmgroup\" value=\"1\">\n";
	}
?>
</table>
</td><td valign="top">
<?
	# Mitarbeiter und Rolle suchen, falls eine ausgewaehlt wurde
	if ($matopr > 0) {
	   $query = "select ma_minum, ma_role from matopr ";
	   $query .= "where ma_num = $matopr";

	   if (!($result = QueryDB($db, $query))) {
	      Error("Interner Fehler: Mitarbeiter und Rolle wurde nicht gefunden!");
	      closeDB($db);
	      return;
	   }

	   if (numrowsDB($result) > 0) {
	      $data = fetchDB($result, 0);
	      $ma_minum = $data[0];
	      $ma_role = $data[1];
	   } else {
	      $ma_minum = 0;
	      $ma_role = 0;
	   }
	}
?>
<table class="input">
  <tr>
    <td colspan=3 align="center">Benutzer und Rolle dem Projekt zuordnen:</td>
  </tr>
  <tr>
    <td><select name="mitarbeiter">
<?
	$query = "select mi_num, mi_nname, mi_vname from mitarbeiter ";
	$query .= "order by mi_nname, mi_vname";

	if (!($result = QueryDB($db, $query))) {
	   Error("Interner Fehler: Es konnten keine Mitarbeiter gefunden werden!");
	   closeDB($db);
	   return;
	}

	$anz = numrowsDB($result);
	$i = 0;

	while ($i < $anz) {
	   $data = fetchDB($result, $i);
	   $mi_num = $data[0];
	   $mi_nname = $data[1];
	   $mi_vname = $data[2];
	   echo "<option value=\"$mi_num\"";

	   if ($ma_minum == $mi_num)
	      echo " selected";

	   echo ">$mi_nname $mi_vname</option>\n";
	   $i++;
	}
?>
      </select>
    </td>
    <td>
<?
	if ($role) {
?>
      <select name="role">
<?
	   $query = "select kr_num, kr_role from key_role order by kr_role";

	   if (!($result = QueryDB($db, $query))) {
	      Error("Interner Fehler: Es konnten keine Mitarbeiterrollen gefunden werden!");
	      closeDB($db);
	      return;
	   }

	   $anz = numrowsDB($result);
	   $i = 0;

	   while ($i < $anz) {
	      $data = fetchDB($result, $i);
	      $kr_num = $data[0];
	      $kr_role = $data[1];
	      echo "<option value=\"$kr_num\"";

	      if ($ma_role == $kr_num)
		 echo " selected";

	      echo ">$kr_role</option>\n";
	      $i++;
	   }
?>
      </select>
<?
	} else {
	   echo "&nbsp;";
	}
?>
    </td>
    <td>
<?
	$lines = "num=$pjnum&name='+edit.name.value+'&pl=";
	$lines .= "'+edit.pl.value+'&plav='+edit.plav.value+'&sgf=";
	$lines .= "'+edit.sgf.value+'&tmgroup='+edit.tmgroup.value+";
	$lines .= "'&status='+edit.status.value+'&mitarbeiter='";
	$lines .= "+edit.mitarbeiter.value+'&role='+edit.role.value+'";
	ButtonLink("Hinzuf&uuml;gen", "edit_pj.php", "pjedit=$pjnum&unum=$unum&headline=$headline&menu=$menu&rstufe=$rstufe&func=matopr&$lines");
?>
    </td>
  </tr>
  <tr>
     <td colspan=2>
       <select size=6 name="matopr" onClick="javascript:MoveTo('edit_pj.php','<? echo "pjedit=$pjnum&unum=$unum&headline=$headline&menu=$menu&rstufe=$rstufe&func=matoprup&matopr='+this.value+'&$lines"; ?>')">
<?
	$query = "select ma_num, mi_nname, mi_vname, kr_role from matopr ";
	$query .= "left join mitarbeiter on mi_num = ma_minum ";
	$query .= "left join key_role on kr_num = ma_role ";
	$query .= "where ma_prnum = $pjnum ";
	$query .= "order by mi_nname, mi_vname";

	if (!($result = QueryDB($db, $query))) {
	   Error("Interner Fehler: Keine Mitarbeiterzuordnungen gefunden!");
	   closeDB($db);
	   return;
	}

	$anz = numrowsDB($result);
	$i = 0;

	while ($i < $anz) {
	   $data = fetchDB($result, $i);
	   $ma_num = $data[0];
	   $mi_nname = $data[1];
	   $mi_vname = $data[2];
	   $kr_role = $data[3];
	   echo "<option value=\"$ma_num\"";

	   if ($matopr == $ma_num)
	      echo " selected";

	   if ($role)
	      echo ">$mi_nname $mi_vname, &lt;$kr_role&gt;</option>\n";
	   else
	      echo ">$mi_nname $mi_vname</option>\n";

	   $i++;
	}
?>
      </select>
    </td>
    <td valign="bottom">
<?
	ButtonLink("L&ouml;schen", "edit_pj.php", "pjedit=$pjnum&unum=$unum&headline=$headline&menu=$menu&rstufe=$rstufe&func=matoprdel&matopr='+edit.matopr.value+'");
?>
    </td>
  </tr>
</table>
</td></tr></table>
<table class="indent"><tr><td>
<?
	ButtonSubmit("Speichern", "edit");
?>
</td></tr></table>
</form>
<?
	closeDB($db);
}

function CheckNewPj($unum, $headline, $menu, $rstufe) {
	$pr_num = $_REQUEST['num'];
	$pr_name = $_REQUEST['name'];
	$pr_pl = $_REQUEST['pl'];
	$pr_plav = $_REQUEST['plav'];
	$pr_sgf = $_REQUEST['sgf'];
	$pr_tmgroup = $_REQUEST['tmgroup'];
	$pr_status = $_REQUEST['status'];
	$err = 0;

	if ($pr_num < 1) {
	   Error("Sie m&uuml;ssen eine Projektnummer eingeben!\n");
	   $err = 1;
	}

	if (!strlen($pr_name)) {
	   Error("Sie m&uuml;ssen eine Projektbezeichnung eingeben!\n");
	   $err = 1;
	}

	$db = OpenDB();
	$query = "select pr_name from project where pr_num = $pr_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows != 0) {
	   Error("Ein Projekt mit der Nummer \"$pr_num\" existiert bereits!\n");
	   $err = 1;
	}

	$query = "select pr_num from project where pr_name = '$pr_name'";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows != 0) {
	   Error("Ein Projekt mit dem Namen \"<i>$pr_name</i>\" existiert bereits!\n");
	   $err = 1;
	}

	if ($err == 1) {
	   EditPj(0, $unum, $headline, $menu, $rstufe);
	   return false;
	}

	# An dieser Stelle haben wir eine gepruefte Datenbasis, welche wir
	# als neuen Datensatz speichern koennen.
	if (!QueryDB($db, "begin")) {
	   closeDB($db);
	   return false;
	}
	
	$query = "insert into project (pr_num, pr_name, pr_pl, pr_plav, pr_sgf, pr_tmgroup, pr_status) ";
	$query = $query . "values ($pr_num, '$pr_name', $pr_pl, $pr_plav, $pr_sgf, $pr_tmgroup, $pr_status)";

	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return false;
	}
	
	# Handelt es sich um ein "Konto" dann muessen wir einen versteckten
	# Plan anlegen. Um Den Plan deutlich als Pseudoplan zu kennzeichnen
	# bekommt er den Status 0.
	if ($pr_status == 1) {
	   $query = "select co_plan, co_task from counter";

	   if (!($result = QueryDB($db, $query))) {
	      QueryDB($db, "rollback");
	      closeDB($db);
	      return false;
	   }

	   $data = fetchDB($result, 0);
	   $co_plan = $data[0] + 1;
	   $co_task = $data[1] + 1;
	   $query = "insert into plan (pl_num, pl_lfd, pl_prnum, pl_status) ";
	   $query .= "values ($co_plan, 0, $pr_num, 0)";

	   if (!QueryDB($db, $query)) {
	      QueryDB($db, "rollback");
	      closeDB($db);
	      return false;
	   }

	   $ta_hash = md5($co_task);
	   $query = "insert into task (ta_num, ta_plnum, ta_id, ta_name, ";
	   $query .= "ta_meeting, ta_start, ta_duration, ta_level, ta_phase, ta_hash) ";
	   $query .= "values ($co_task, $co_plan, 0, '$pr_name', 0, 0, ";
	   $query .= "-32767, 0, 0, '$ta_hash')";

	   if (!QueryDB($db, $query)) {
	      QueryDB($db, "rollback");
	      closeDB($db);
	      return false;
	   }

	   $query = "update counter set co_plan = $co_plan, co_task = $co_task";

	   if (!QueryDB($db, $query)) {
	      QueryDB($db, "rollback");
	      closeDB($db);
	      return false;
	   }
	}

	QueryDB($db, "commit");
	Journal(203, "Projekt: $pr_num $pr_name", $db);

	if ($pr_status == 1)
	   Journal(204, "Plan: $co_plan; Projekt: $pr_num", $db);

	closeDB($db);
	return true;
}

function CheckEditPj($unum, $headline, $menu, $rstufe) {
	$pr_num = $_REQUEST['num'];
	$pr_name = $_REQUEST['name'];
	$pr_pl = $_REQUEST['pl'];
	$pr_plav = $_REQUEST['plav'];
	$pr_sgf = $_REQUEST['sgf'];
	$pr_tmgroup = $_REQUEST['tmgroup'];
	$pr_status = $_REQUEST['status'];
	$err = 0;

	if ($pr_num < 1) {
	   Error("Sie m&uuml;ssen eine Projektnummer eingeben!\n");
	   $err = 1;
	}

	if (!strlen($pr_name)) {
	   Error("Sie m&uuml;ssen eine Projektbezeichnung eingeben!\n");
	   $err = 1;
	}

	$db = OpenDB();
	$query = "select pr_name from project where pr_num = $pr_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows != 1) {
	   Error("Ein Projekt mit der Nummer \"$pr_num\" existiert nicht!\n");
	   $err = 1;
	}

	if ($err == 1) {
	   EditPj(0, $unum, $headline, $menu, $rstufe);
	   return false;
	}

	# An dieser Stelle haben wir eine gepruefte Datenbasis, welche wir
	# speichern koennen.
	if (!QueryDB($db, "begin")) {
	   closeDB($db);
	   return false;
	}

	$query = "update project set pr_name = '$pr_name', pr_pl = $pr_pl, pr_plav = $pr_plav, ";
	$query .= "pr_sgf = $pr_sgf, pr_tmgroup = $pr_tmgroup, pr_status = $pr_status ";
	$query .= "where pr_num = $pr_num";
	
	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return false;
	}

	Journal(301, "Projekt: $pr_num $pr_name", $db);
	closeDB($db);
	return true;
}

function AskDelete ($pr_num) {
	$headline = $_REQUEST['headline'];
	$menu = $_REQUEST['menu'];

	$nav = "pr_num=$pr_num&menu=$menu&";
	$nav .= "headline=$headline&func=DeletePj";

	# Projektnamen auslesen
	$db = OpenDB();
	$query = "select pr_name from project where pr_num = $pr_num";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$pr_name = $data[0];
	closeDB($db);
?>
<br>
<form name="Alarm" method="post">
<table border=2 class="alarm">
   <tr><td colspan=2 style="text-align: center"><big>Warnung!</big><br><br>
           Wollen Sie wirklich das Projekt<br>
	   <b><? echo "$pr_num $pr_name"; ?></b><br>
	   unwiederbringlich l&ouml;schen?<br>
	   Es werden alle Pl&auml;ne dieses Projekts und alle
	   eventuell angefallenen Aufw&auml;nde der Mitarbeiter
	   automatisch gel&ouml;scht!!<br><br>
	   Projekt unwiederbringlich l&ouml;schen?
      </td>
   </tr>
   <tr>
      <td><center><input type="button" name="yes" value="L&ouml;schen" onClick="javascript:MoveTo('edit_pj.php','yes=yes&<? echo "$nav"; ?>')"></center></td>
      <td><center><input type="button" name="no" value="Abbruch" onClick="javascript:MoveTo('edit_pj.php','no=no&<? echo "$nav"; ?>')"></center></td>
   </tr>
</table>
</form>
<?php
}

function DeleteProject($pr_num) {
	global $rstufe;

	if ($rstufe != 1) {
	   Error("Sie sind nicht berechtigt ein Projekt zu l&ouml;schen!");
	   return false;
	}

	if (!isset($pr_num) || $pr_num <= 0) {
	   Error("Interner Fehler: Projektnummer fehlt!");
	   return false;
	}

	$db = OpenDB();

	if (!QueryDB($db, "begin")) {
	   closeDB($db);
	   return false;
	}

	# Als erstes loeschen wir eventuell vorhandene Kalendereintraege
	$query = "delete from calendar where ca_prnum = $pr_num";

	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return false;
	}

	# Hier loeschen wir die IST-Erfassungen
	$query = "delete from wdone where wd_prnum = $pr_num";

	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return false;
	}

	$query = "select pl_num from plan where pl_prnum = $pr_num";

	if (!($respl = QueryDB($db, $query))) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return false;
	}

	$numpl = numrowsDB($respl);
	$cnumpl = 0;

	while ($cnumpl < $numpl) {
	   $data = fetchDB($respl, $cnumpl);
	   $pl_num = $data[0];
	   $query = "select ta_num from task where ta_plnum = $pl_num";

	   if (!($resta = QueryDB($db, $query))) {
	      QueryDB($db, "rollback");
	      closeDB($db);
	      return false;
	   }

	   $numta = numrowsDB($resta);
	   $cnumta = 0;

	   while ($cnumta < $numta) {
	      $data = fetchDB($resta, $cnumta);
	      $ta_num = $data[0];
	      # Hier loeschen wir die Ressourcen zum Projekt
	      $query = "delete from allocation where al_task = $ta_num";

	      if (!QueryDB($db, $query)) {
	         QueryDB($db, "rollback");
	         closeDB($db);
	         return false;
	      }

	      $cnumta++;
	   }

	   # Hier loeschen wir die Tasks des Projekts
	   $query = "delete from task where ta_plnum = $pl_num";

	   if (!QueryDB($db, $query)) {
	      QueryDB($db, "rollback");
	      closeDB($db);
	      return false;
	   }

	   $cnumpl++;
	}

	# Hier loeschen wir die Plaene des Projekts
	$query = "delete from plan where pl_prnum = $pr_num";

	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return false;
	}

	# Hier loeschen wir die Mitarbeiter des Projekts
	$query = "delete from matopr where ma_prnum = $pr_num";

	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return false;
	}

	# Schliesslich koennen wir das Projekt selbst loeschen
	$query = "delete from project where pr_num = $pr_num";

	if (!QueryDB($db, $query)) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return false;
	}

	# Jetzt da alles geloescht ist, wird es fix gemacht
	QueryDB($db, "commit");
	closeDB($db);
	return true;
}

function SaveMaToPr() {
	$mitarbeiter = $_REQUEST['mitarbeiter'];
	$role = $_REQUEST['role'];
	$pr_num = $_REQUEST['num'];

	if (!isset($mitarbeiter) || !isset($role) || $mitarbeiter < 1 || $role < 1) {
	   Error("Interner Fehler: Ein Parameter fehlt oder ist ung&uuml;ltig!");
	   return;
	}

	if (!isset($pr_num) || $pr_num < 1) {
	   Error("Sie m&uuml;ssen eine Projektnummer eintragen!");
	   return;
	}

	$db = OpenDB();
	$query = "select count(*) from matopr where ma_minum = $mitarbeiter and ";
	$query .= "ma_prnum = $pr_num";

	if (!($result = QueryDB($db, $query))) {
	   Error("Interner Fehler: Datens&auml;tze konnten nicht gez&auml;hlt werden!");
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);

	if ($data[0] > 0)
	   $flag = true;
	else
	   $flag = false;

	$query = "select co_matopr from counter";

	if (!($result = QueryDB($db, $query))) {
	   Error("Interner Fehler: Z&auml;hler nicht gefunden!");
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$co_matopr = $data[0] + 1;

	if (!$flag) {
	   $query = "insert into matopr (ma_num, ma_minum, ma_prnum, ma_role) ";
	   $query .= "values ($co_matopr, $mitarbeiter, $pr_num, $role)";
	} else {
	   $query = "update matopr set ma_role = $role where ";
	   $query .= "ma_minum = $mitarbeiter and ma_prnum = $pr_num";
	}

	if (!QueryDB($db, "begin")) {
	   Error("Interner Fehler: Transaktion fehlgeschlagen!");
	   closeDB($db);
	   return;
	}

	if (!QueryDB($db, $query)) {
	   Error("Interner Fehler: Konnte Datensatz nicht einf&uuml;gen!");
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return;
	}

	if (!$flag) {
	   $query = "update counter set co_matopr = $co_matopr";

	   if (!QueryDB($db, $query)) {
	      Error("Interner Fehler: Z&auml;hlerstand konnte nicht erh&ouml;ht werden!");
	      QueryDB($db, "rollback");
	      closeDB($db);
	      return;
	   }
	}

	QueryDB($db, "commit");
	closeDB($db);
}

function DeleteMaToPr() {
	$ma_num = $_REQUEST['matopr'];

	if (!isset($ma_num) || $ma_num < 1) {
	   Error("Interner Fehler: Falsche oder fehlende Referenznummer!");
	   return;
	}

	$db = OpenDB();
	$query = "select ma_minum, ma_prnum from matopr where ma_num = $ma_num";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	if (numrowsDB($result) <= 0) {
	   Error("Interner Fehler: Referenznummer wurde nicht gefunden!");
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$ma_minum = $data[0];
	$ma_prnum = $data[1];

	$query = "select count(*) from allocation ";
	$query .= "left join task on ta_num = al_task ";
	$query .= "left join plan on pl_num = ta_plnum ";
	$query .= "where al_ressource = $ma_minum and pl_prnum = $ma_prnum";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$anz = $data[0];

	if ($anz > 0) {
	   Error("Mitarbeiter ist bereits im Projekt verplant und kann daher nicht gel&ouml;scht werden!");
	   closeDB($db);
	   return;
	}

	$query = "delete from matopr where ma_num = $ma_num";

	if (!QueryDB($db, $query)) {
	   closeDB($db);
	   return;
	}

	closeDB($db);
}

$pjnew = $_REQUEST['pjnew'];
$pjedit = $_REQUEST['pjedit'];
$pjdel = $_REQUEST['pjdel'];
$func = $_REQUEST['func'];
$yes = $_REQUEST['yes'];

if (isset($func)) {
   if ($func == "CheckNewPj") {
      if (CheckNewPj($unum, $headline, $menu, $rstufe)) {
         echo "<p>Daten wurden erfolgreich gespeichert!</p>\n";
	 echo "<form name=\"forms\" action=\"forms.php\" method=\"post\">";
	 echo "<input type=\"hidden\" name=\"unum\" value=\"$unum\">";
	 echo "<input type=\"hidden\" name=\"headline\" value=\"4\">";
	 echo "<input type=\"hidden\" name=\"menu\" value=\"$menu\">";
	 echo "<input type=\"hidden\" name=\"rstufe\" value=\"$rstufe\">";
	 echo "<input type=\"hidden\" name=\"knopf\" value=\"31\">";
	 Button("Weiter -->", "forms");
//	 echo "<input type=\"submit\" name=\"submit\" value=\"Weiter -->\">";
	 echo "</form>\n";
      }
   }

   if ($func == "CheckEditPj") {
      if (CheckEditPj($unum, $headline, $menu, $rstufe)) {
         echo "<p>Daten wurden erfolgreich gespeichert!</p>\n";
	 echo "<form name=\"forms\" action=\"forms.php\" method=\"post\">";
	 echo "<input type=\"hidden\" name=\"unum\" value=\"$unum\">";
	 echo "<input type=\"hidden\" name=\"headline\" value=\"4\">";
	 echo "<input type=\"hidden\" name=\"menu\" value=\"$menu\">";
	 echo "<input type=\"hidden\" name=\"rstufe\" value=\"$rstufe\">";
	 echo "<input type=\"hidden\" name=\"knopf\" value=\"31\">";
	 Button("Weiter -->", "forms");
//	 echo "<input type=\"submit\" name=\"submit\" value=\"Weiter -->\">";
	 echo "</form>\n";
      }
   }

   if ($func == "DeletePj" && isset($yes) && $yes == "yes") {
      $pr_num = $_REQUEST['pr_num'];
      if (DeleteProject($pr_num)) {
         echo "<p>Projekt $pr_num wurde unwiederbringlich gel&ouml;scht!</p>\n";
         Journal(102, "Projekt: $pr_num");
      }

      echo "<form name=\"forms\" action=\"forms.php\" method=\"post\">";
      echo "<input type=\"hidden\" name=\"unum\" value=\"$unum\">";
      echo "<input type=\"hidden\" name=\"headline\" value=\"4\">";
      echo "<input type=\"hidden\" name=\"menu\" value=\"$menu\">";
      echo "<input type=\"hidden\" name=\"rstufe\" value=\"$rstufe\">";
      echo "<input type=\"hidden\" name=\"knopf\" value=\"31\">";
      Button("Weiter -->", "forms");
//      echo "<input type=\"submit\" name=\"submit\" value=\"Weiter -->\">";
      echo "</form>\n";
   }

   if ($func == "matopr")
      SaveMaToPr();

   if ($func == "matoprdel")
      DeleteMaToPr();
}

if (isset($pjnew)) {
   NewPj($unum, $headline, $menu, $rstufe);
}

if (isset($pjedit)) {
   EditPj($pjedit, $unum, $headline, $menu, $rstufe);
}

if (isset($pjdel)) {
   AskDelete($pjdel);
}

require('footer.inc');
?>
